package services;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;

public class DicT extends JFrame implements Runnable{
	public static String dictFile = "Dictionary";
	public static Vector<Vector<String>> dataV = new Vector<Vector<String>>();
	
	private Vector<String> columnIdentifier;
	private DicTableModel dtm;
	private JTable table;
	private JTextField txtFind;
	private CsvReader csvReader;
	private CsvWriter csvWriter;
	
	public DicT() {
		setSize(500,413);
		setTitle("Alias definition");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		columnIdentifier = new Vector<String>();		
		columnIdentifier.add("Course name");
		columnIdentifier.add("Alias (separate by ,)");
		
		JScrollPane scrollPane = new JScrollPane();
		
		txtFind = new JTextField();
		txtFind.setText("Find ...");
		txtFind.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(txtFind, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
						.addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(txtFind, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
					.addContainerGap())
		);
		//dataVector = new Vector<Vector>();		
		
		table = new JTable();
		dtm = new DicTableModel();
		table.setModel(dtm);

		dtm.setDataVector(dataV, columnIdentifier);
		
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		
		addWindowListener(new WindowListener(){

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				saveDicT();
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}	
	
	private void initialData(){
		if (DataCache.vCourseName.size()>0){
			for (int cr=0; cr<DataCache.vCourseName.size(); cr++){
				dataV.addElement(new Vector<String>());
				dataV.lastElement().add(DataCache.vCourseName.elementAt(cr));
				dataV.lastElement().add(getAbbr(DataCache.vCourseName.elementAt(cr)));
			}
		}
		dtm.setDataVector(dataV, columnIdentifier);
		setVisible(true);

	}
	
	private String getAbbr(String input){
		String output = "";
		String sCompany = "";
		String sProductName = "";
		String sVersion = "";
		String sOther = "";
		
		
		Pattern pn;
		Matcher mr;
		
                pn = Pattern.compile("([a-z].*)\\ ([0-9]+\\.[0-9x]{0,1}|[0-9]{1,4})\\ (.*)", Pattern.CASE_INSENSITIVE);
		mr = pn.matcher(input);
		if(mr.matches()){
			//mr.group(1) ��������������
			sProductName = mr.group(1);
			sVersion = mr.group(2);
			sOther = mr.group(3);
			//System.out.println("p:" + sProductName + " v:" +sVersion);
		}
		while (true){
			pn = Pattern.compile(".*(Admin)istration.*");//|()|");
			mr = pn.matcher(sOther);
			if (mr.matches()){
				sOther = mr.group(1);
				break;
			}
			
			pn = Pattern.compile(".*(Diff)erence.*");
			mr = pn.matcher(sOther);
			if (mr.matches()){
				sOther = mr.group(1);
				break;
			}
			
			pn = Pattern.compile(".*(Tech)nical.*");
			mr = pn.matcher(sOther);
			if (mr.matches()){
				sOther = mr.group(1);
				break;
			}

			break;
		}
		
		while (true){
			//(Symantec, Veritas)
			pn = Pattern.compile("^(sym){1}(antec){0,1}\\ (.*)",Pattern.CASE_INSENSITIVE);
			mr = pn.matcher(sProductName);
			if (mr.matches()){
				sCompany = "Symantec";
				break;
			}
			break;
		}
		

		char eachChar[] = sProductName.toCharArray();
		for (int ic=0; ic<eachChar.length; ic++){
			if (eachChar[ic] > 64 && eachChar[ic] < 91){
				output += eachChar[ic];
			}
		}
		output = sCompany + " " + output + " " + sVersion + " " +sOther;
		return output;
	}
	
	public static String parseCourseName(String input){
		String output = input;
		Pattern p;
		Matcher m;
		
		p = Pattern.compile("BE");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Backup Exec ");
			return output;
		}
		p = Pattern.compile("NBU");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("NetBackup ");
			return output;
		}
		p = Pattern.compile("SF");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Storage Foundation ");
			return output;
		}
		p = Pattern.compile("EV");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Enterprise Vault ");
			return output;
		}
		p = Pattern.compile("VCS");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Veritas Cluster Server ");
			return output;
		}
		p = Pattern.compile("SEP");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Symantec Endpoint Protection ");
			return output;
		}
		p = Pattern.compile("CCS");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Control Compliance Suite ");
			return output;
		}
		p = Pattern.compile("DLP");
		m = p.matcher(input);
		if (m.find()){
			output = m.replaceAll("Data Loss Prevention ");
			return output;
		}

		return output;
	}

	private void loadDicT(){
		csvReader = null;
		//FileInputStream fis = null;
		try {
			csvReader = new CsvReader(dictFile);
			csvReader.readHeaders();
			//int index = 0;
			while (csvReader.readRecord()){
				dataV.addElement(new Vector<String>());
				dataV.lastElement().add(csvReader.get("Key"));
				dataV.lastElement().add(csvReader.get("Value"));
				//index ++;
			}
			//System.out.println(dataV);
			dtm.setDataVector(dataV, columnIdentifier);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (csvReader != null){
				csvReader.close();
			}
		}
		
	}
	
	private void saveDicT(){
		csvWriter = null;
		java.io.File test = new java.io.File(dictFile);
		try {
			csvWriter = new CsvWriter(new FileWriter(dictFile, true), ',');
			if (!test.exists()){
				csvWriter.write("Key");
				csvWriter.write("Value");
				csvWriter.endRecord();
			}
			if (dtm.getDataVector().size()>0){
				dataV.removeAllElements();
				dataV.addAll(dtm.getDataVector());
				for (int r=0; r<dataV.size(); r++){
					for (int c=0; c<dataV.elementAt(r).size(); c++){
						csvWriter.write(dataV.elementAt(r).elementAt(c));
					}
					csvWriter.endRecord();
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			csvWriter.close();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		/*java.io.File isExists = new java.io.File(dictFile);
		if (isExists.exists()){
			loadDicT();
		}
		else{
			initialData();
		}*/
	}
}

class DicTableModel extends DefaultTableModel{
	@Override
	public boolean isCellEditable(int row, int column) {
		if (column > 0){
			return true;
		}else{
			return false;
		}
    }
}
